package org.helyx.gae.getabike.provider;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.helyx.gae.getabike.content.provider.exception.ContentProviderException;
import org.helyx.gae.getabike.localization.LocalizationUtil;
import org.helyx.gae.getabike.localization.Point;
import org.helyx.gae.getabike.model.Station;
import org.helyx.gae.getabike.provider.normalizer.StationInfoNormalizer;
import org.helyx.gae.getabike.xml.ElementAttributeAccessor;

public class VelibStationContentProvider extends AbstractStationContentProvider {

	private static final String ELT_MARKER = "/carto/markers/marker";
	
	private static final String ATTR_NUMBER = "number";
	private static final String ATTR_NAME = "name";
	private static final String ATTR_ADDRESS = "address";
	private static final String ATTR_FULL_ADDRESS = "fullAddress";
	private static final String ATTR_OPEN = "open";
	private static final String ATTR_BONUS = "bonus";
	private static final String ATTR_LNG = "lng";
	private static final String ATTR_LAT = "lat";
	
	private static final int OPEN_VALUE = 1;

	public VelibStationContentProvider() {
		super();
	}

	protected List<Station> getStationList(InputStream inputStream) throws ContentProviderException {

		List<Station> stationList = new ArrayList<Station>();
		try {
			SAXReader reader = new SAXReader();
	        Document document = reader.read(inputStream);
			
			@SuppressWarnings("unchecked")
			List<Element> list = (List<Element>)document.selectNodes(ELT_MARKER);

			StationInfoNormalizer stationNameNormalizer = getStationInfoNormalizer();
			ElementAttributeAccessor eltAttributeAccessor = new ElementAttributeAccessor();

			for (Element markerElt : list) {

				eltAttributeAccessor.selectElement(markerElt);
				Station station = new Station();
				station.setLocalization(new Point());

				station.setNumber(eltAttributeAccessor.getAttrValueAsInt(ATTR_NUMBER));
				station.setName(eltAttributeAccessor.getAttrValueAsString(ATTR_NAME));
				
				station.setOpen(city.isState() ? eltAttributeAccessor.getAttrValueAsInt(ATTR_OPEN) == OPEN_VALUE : true);
				station.setBonus(city.isBonus() ? eltAttributeAccessor.getAttrValueAsBoolean(ATTR_BONUS) : false);
				station.setTpe(false);
				station.setAddress(eltAttributeAccessor.getAttrValueAsString(ATTR_ADDRESS));
				station.setFullAddress(eltAttributeAccessor.getAttrValueAsString(ATTR_FULL_ADDRESS).trim());
				station.getLocalization().setLat(eltAttributeAccessor.getAttrValueAsDouble(ATTR_LAT));
				station.getLocalization().setLng(eltAttributeAccessor.getAttrValueAsDouble(ATTR_LNG));
				station.setHasLocalization(LocalizationUtil.isSet(station.getLocalization()));

				if (stationNameNormalizer != null) {
					stationNameNormalizer.normalizeName(station);
				}
				
				stationList.add(station);
			}			
		}
		catch(DocumentException e) {
			throw new ContentProviderException(e);
		}

		return stationList;
	}

}
